package com.example.newproject_overseas.util.imageUtil


import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Log
import android.widget.ImageView
import androidx.core.content.FileProvider
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.example.newproject_overseas.R
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.interfaces.OnCallbackListener
import com.luck.picture.lib.utils.ActivityCompatHelper
import java.io.File

/**
 * 在项目中创建一个GlideEngine类
 * Glide加载引擎
 */
class GlideEngine : ImageEngine {

    /**
     * 加载图片
     *
     * @param context   上下文
     * @param url       资源url
     * @param imageView 图片承载控件
     */
    override fun loadImage(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return;
        }
        Glide.with(context)
            .load(url)
            .into(imageView);
    }

    /**
     * 加载指定url并返回bitmap
     *
     * @param context   上下文
     * @param url       资源url
     * @param maxWidth  资源最大加载尺寸
     * @param maxHeight 资源最大加载尺寸
     * @param call      回调接口
     */
    override fun loadImageBitmap(
        context: Context,
        url: String,
        maxWidth: Int,
        maxHeight: Int,
        call: OnCallbackListener<Bitmap>?,
    ) {

        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        Glide.with(context)
            .asBitmap()
            .override(maxWidth, maxHeight)
            .load(url)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {
                    call?.onCall(resource)
                }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    call?.onCall(null)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                }
            })
    }



    /**
     * 加载相册目录封面
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    override fun loadAlbumCover(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return;
        }

        Log.i("context.packageName","${context.packageName}")


        val imageUri = Uri.parse(url) // Convert the URL to a Uri

        Glide.with(context)
            .asBitmap()
            .load(imageUri) // Use the Uri to load the image
            .override(180, 180)
            .sizeMultiplier(0.5f)
            .into(object : BitmapImageViewTarget(imageView) {
                override fun setResource(resource: Bitmap?) {
                    resource?.let {
                        val circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.resources, it)
                        circularBitmapDrawable.cornerRadius = 8f
                        imageView.setImageDrawable(circularBitmapDrawable)
                    }
                }
            })
    }

    override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        Glide.with(context)
            .load(url)
            .override(200, 200)
            .centerCrop()
            //.placeholder(R.drawable.ps_image_placeholder)
            .into(imageView)
    }

    override fun pauseRequests(context: Context?) {
        if (context != null) {
            Glide.with(context).pauseRequests()
        }
    }

    override fun resumeRequests(context: Context?) {
        if (context != null) {
            Glide.with(context).resumeRequests()
        }
    }

    // 私有化构造函数，防止外部创建实例
    private constructor()

    companion object{
        // 在Kotlin中，单例对象的创建和获取是自动线程安全的，不需要使用双重检查锁定
        private object Holder {
            val INSTANCE = GlideEngine()
        }


        // 获取GlideEngine实例的方法
        fun createGlideEngine(): GlideEngine {
            return Holder.INSTANCE
        }
    }
}